package cn.wjdiankong.levideo.database;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import cn.wjdiankong.levideo.data.LevideoData;
import cn.wjdiankong.levideo.utils.Logger;

public class LocalStorage {
    private static LocalStorage sInstance = new LocalStorage();

    public final static String TAG = "LocalStorage";

    private LocalStorage() {
    }

    public static LocalStorage getInstance() {
        return sInstance;
    }

    private Context context = null;

    public void setContext(Context context) {
        this.context = context.getApplicationContext();
    }


    private SQLiteDatabase getDatabase() {
        SQLiteDatabase db = Main_DbHelper.getInstance(context).getWritableDatabase();
        return db;
    }

    public List<LevideoData> getLevideoDataList(){
        List<LevideoData> list = new ArrayList<LevideoData>();
        SQLiteDatabase db = getDatabase();
        Cursor cursor = null;
        try{
            cursor = db.query(LevideoData.TABLE_NAME, null, null , null, null, null, null);
            while (cursor.moveToNext()){
            	LevideoData data = new LevideoData();
                list.add(data.fromCursor(cursor));
            }
        }catch(Exception e){
            Logger.log("getAdsDataList error..."+e.toString());
        }finally {
            if(cursor != null){
                cursor.close();
            }
        }
        return list;
    }
    
    public List<LevideoData> getLevideoDataList(int type){
        List<LevideoData> list = new ArrayList<LevideoData>();
        SQLiteDatabase db = getDatabase();
        Cursor cursor = null;
        String wheres = LevideoData.Columns.VIDEO_TYPE + "=?";
        try{
            cursor = db.query(LevideoData.TABLE_NAME, null, wheres , new String[]{type+""}, null, null, null);
            while (cursor.moveToNext()){
            	LevideoData data = new LevideoData();
                list.add(data.fromCursor(cursor));
            }
        }catch(Exception e){
            Logger.log("getAdsDataList error..."+e.toString());
        }finally {
            if(cursor != null){
                cursor.close();
            }
        }
        return list;
    }

    public boolean saveLevideoDataList(List<LevideoData> xs){
        if(xs == null || xs.size() == 0){
            return false;
        }
        boolean flag = false;
        SQLiteDatabase db = getDatabase();
        try{
            db.beginTransaction();
            for(LevideoData data : xs){
            	saveLevideoData(db, data);
            }
            db.setTransactionSuccessful();
        }catch (Exception e){
            Logger.log(Log.getStackTraceString(e));
        }finally {
            try{
                db.endTransaction();
            }catch (Exception e){
                Logger.log(Log.getStackTraceString(e));
            }
        }
        return flag;
    }

    private boolean saveLevideoData(SQLiteDatabase db, LevideoData data){
        boolean flag = true;
        if(data == null){
            return false;
        }
        try{
        	db.insert(LevideoData.TABLE_NAME,null,data.toContentValues());
        }catch(Exception e){
        	Logger.log("save data err:"+Log.getStackTraceString(e));
        	return false;
        }
        return flag;
    }
    
    public boolean deleteLevideoData(int type){
    	SQLiteDatabase db = getDatabase();
    	String wheres = LevideoData.Columns.VIDEO_TYPE + "=?";
        try{
        	db.delete(LevideoData.TABLE_NAME, wheres, new String[]{type+""});
        	return true;
        }catch(Exception e){
        	Logger.log("delete data err:"+Log.getStackTraceString(e));
        	return false;
        }
    }
    
    public boolean deleteLevideoData(String path){
    	SQLiteDatabase db = getDatabase();
    	String wheres = LevideoData.Columns.LOCAL_PATH + "=?";
        try{
        	db.delete(LevideoData.TABLE_NAME, wheres, new String[]{path});
        	return true;
        }catch(Exception e){
        	Logger.log("delete data err:"+Log.getStackTraceString(e));
        	return false;
        }
    }
    
    public boolean deleteAllLevideoData(){
    	SQLiteDatabase db = getDatabase();
        try{
        	db.delete(LevideoData.TABLE_NAME, null, null);
        	return true;
        }catch(Exception e){
        	Logger.log("delete data err:"+Log.getStackTraceString(e));
        	return false;
        }
    }
    
    public boolean saveLevideoData(LevideoData data){
        boolean flag = true;
        if(data == null){
            return false;
        }
        SQLiteDatabase db = getDatabase();
        try{
        	db.insert(LevideoData.TABLE_NAME, null, data.toContentValues());
        }catch(Exception e){
        	Logger.log("save data err:"+Log.getStackTraceString(e));
        	return false;
        }
        return flag;
    }

}
